<?php

class UserAction extends HomeCommonAction {

    public function showMyCollection() {
        $this->checkLogin();
        $this->display('my_collection');
    }

    public function my() {
        $this->checkLogin();
        $condition = array();
        $uid = Session::getLocal('id');
        $condition['id'] = $uid;
        $User = D('User');
        $user_info = $User->where($condition)->find();

        $condition = array();
        $condition['userid'] = $uid;
        $Question = D('Question');
        $question_count = $Question->where($condition)->count();
        $Share = D("Share");
        $share_count = $Share->where($condition)->count();

        $UserFlow = D('UserFlow');
        //        该用户关注的
        $user_flow = $UserFlow->where("userid = '{$uid}'")->count();
        //        关注该用户的
        $flow_user = $UserFlow->where("flow_userid = '{$uid}'")->count();
        //收藏
        $user_favorites = D("UserFavorites");
        $favorites = $user_favorites->where("userid = '{$uid}'")->count();

        $user_info['question_count'] = $question_count;
        $user_info['share_count'] = $share_count;
        $user_info['user_flow'] = $user_flow;
        $user_info['flow_user'] = $flow_user;

        if (count($user_info) <= 0) {
            return false;
        }
        $this->assign('user_info', $user_info);
        $this->assign('favorites', $favorites);
        $this->assign('user_current', 'current');
        $this->display('user_info');
    }

    public function userInfoAjax() {

        $condition = array();
        $uid = $_GET['userid'];
        $condition['id'] = $uid;
        $User = D('User');
        $user_info = $User->where($condition)->find();

        $condition = array();
        $condition['userid'] = $uid;
        $Question = D('Question');
        $question_count = $Question->where($condition)->count();
        $Share = D("Share");
        $share_count = $Share->where($condition)->count();

        $UserFlow = D('UserFlow');
        //        该用户关注的
        $user_flow = $UserFlow->where("userid = '{$uid}'")->count();
        //        关注该用户的
        $flow_user = $UserFlow->where("flow_userid = '{$uid}'")->count();

        $user_info['question_count'] = $question_count;
        $user_info['share_count'] = $share_count;
        $user_info['user_flow'] = $user_flow;
        $user_info['flow_user'] = $flow_user;

        if (count($user_info) <= 0) {
            return false;
        }
        $this->assign('user_info', $user_info);
        $this->assign('user_current', 'current');
        $this->display('user_info_ajax');
    }

    public function myQuestion() {
        $this->checkLogin();
        import("ORG.Util.String");
        $condition = array();
        $uid = Session::getLocal('id');
        $condition['userid'] = $uid;
        $condition['is_del'] = '0';
        $question_order_str = trim($_GET['question_order']);
        if (empty($question_order_str)) {
            $question_order_str = 'time_add desc';
        } else {
            $question_order_str .= 'desc';
        }
        $Question = D('Question');
        import("ORG.Util.AjaxPage"); // 导入分页类
        $count = $Question->where($condition)->count(); // 查询满足要求的总记录数
        $Page = new AjaxPage($count, 10, 'show_my_question'); // 实例化分页类传入总记录数和每页显示的记录数
        $show = $Page->show(); // 分页显示输出
        $list = $Question->where($condition)->order($question_order_str)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        for ($i = 0; $i < count($list); $i++) {
            $list[$i]['content'] = String::msubstr(strip_tags(html_entity_decode($list[$i]['content'])), $start = 0, 150);
        }
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display('my_question');
    }

    public function myShare() {
        $this->checkLogin();
        import("ORG.Util.String");
        $uid = Session::getLocal('id');
        $share_order_str = trim($_GET['share_order']);

        if (empty($share_order_str)) {
            $share_order_str = 'time_add desc';
        } else {
            $share_order_str .= 'desc';
        }

        $Share = D('Share');
        import("ORG.Util.AjaxPage"); // 导入分页类
        $count = $Share->where("userid = '{$uid}' and is_del = 0 ")->count(); // 查询满足要求的总记录数

        $Page = new AjaxPage($count, 10, 'show_my_share'); // 实例化分页类传入总记录数和每页显示的记录数

        $show = $Page->show(); // 分页显示输出

        $list = $Share->where("userid = '{$uid}' and is_del = 0")->order($share_order_str)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $hangye = F('haneye');
        for ($i = 0; $i < count($list); $i++) {
            $list[$i]['content'] = String::msubstr(strip_tags(html_entity_decode($list[$i]['content'])), $start = 0, 150);
            foreach ($hangye as $one) {
                if ($one['value'] == $list[$i]['industry_id']) {
                    $list[$i]['industry_id'] = $one['name'];
                    break;
                }
            }
        }

        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display('my_share');
    }

    public function myAnswerQuestion() {
        $this->checkLogin();
        import("ORG.Util.String");
        $uid = Session::getLocal('id');
        $share_order_str = trim($_GET['share_order']);



        $QuestionShare = D('QuestionShare');
        import("ORG.Util.AjaxPage"); // 导入分页类
        // $QuestionShare->join(' question ON question_share.question_id = question.id')->join('share ON question_share.share_id = share.id')->where('share.userid = '.$uid);


        $count = $QuestionShare->join(' cm_question ON cm_question_share.question_id = cm_question.id')->join('cm_share ON cm_question_share.share_id = cm_share.id')->where('cm_share.userid = ' . $uid)->order('cm_question_share.ins_time desc')->count(); // 查询满足要求的总记录数

        $Page = new AjaxPage($count, 10, 'show_my_share'); // 实例化分页类传入总记录数和每页显示的记录数

        $show = $Page->show(); // 分页显示输出

        $list = $QuestionShare->join(' cm_question ON cm_question_share.question_id = cm_question.id')->join('cm_share ON cm_question_share.share_id = cm_share.id')->where('cm_share.userid = ' . $uid)->order('cm_question_share.ins_time desc')->limit($Page->firstRow . ',' . $Page->listRows)->field('cm_question.*,cm_question_share.*')->select();
        //        print_r($list);die;
        for ($i = 0; $i < count($list); $i++) {
            $list[$i]['content'] = String::msubstr(strip_tags(html_entity_decode($list[$i]['content'])), $start = 0, 150);
        }

        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display('my_answer_question');
    }

    public function myFavorites() {
        $this->checkLogin();
        $uid = Session::getLocal('id');
        $UserFavorites = D('UserFavorites');
        $favorites = $UserFavorites->where("userid = '{$uid}'")->select();


        $brand_str = array();
        $share_str = array();
        if (count($favorites) > 0) {
            foreach ($favorites as $one) {
                $brand_str[] = $one['brand_id'];
                $share_str[] = $one['share_id'];
            }
        }
        $BaseBrand = D('BaseBrand');
        $condition = array();
        $condition['id'] = array('in', $brand_str);
        $basebrand_info = $BaseBrand->where($condition)->select();

        $Share = D('Share');
        $condition = array();
        $condition['id'] = array('in', $share_str);
        $share_info = $Share->relation(true)->where($condition)->select();


        $UserCommodityScore = D('UserCommodityScore');
        $condition = array();
        $condition['user_id'] = Session::getLocal('id');
        $history_list = $UserCommodityScore->relation(true)->where($condition)->order('commodity_score desc')->group('commodity_id')->limit("0,8")->select();
        $history_list_asc = $UserCommodityScore->relation(true)->where($condition)->order('commodity_score asc')->group('commodity_id')->limit("0,8")->select();
		
        $this->assign('history_list', $history_list);
        $this->assign('history_list_asc', $history_list_asc);
        $this->assign('basebrand_info', $basebrand_info);
        $this->assign('share_info', $share_info);
        $this->assign('list', $list);

        $this->display('my_favorites');
    }

    public function searchFavorites() {
        $this->checkLogin();
        $uid = Session::getLocal('id');
        $UserFavorites = D('UserFavorites');
        $favorites = $UserFavorites->where("userid = '{$uid}'")->select();

        $brand_str = array();
        $share_str = array();
        if (count($favorites) > 0) {
            foreach ($favorites as $one) {
                $brand_str[] = $one['brand_id'];
                $share_str[] = $one['share_id'];
            }
        }
        $BaseBrand = D('BaseBrand');
        $condition = array();
        $condition['id'] = array('in', $brand_str);
        $basebrand_info = $BaseBrand->where($condition)->select();

        $Share = D('Share');
        $condition = array();
        $condition['id'] = array('in', $share_str);

        $brand = $_REQUEST['brand_word'];
//        $brand = iconv('GB2312', 'UTF-8', $brand);
        if (!empty($brand)) {
            $con = array();
            $con['name'] = $brand;
            $BaseBrand = D('BaseBrand');
            $brand = $BaseBrand->where($con)->find();
            $condition['brand_id'] = $brand['id'];
        }
        $category = $_REQUEST['category_word'];
        $category = iconv('GB2312', 'UTF-8', $category);
        if (!empty($category)) {
            $con = array();
            $con['name'] = $category;
            $con['brand_id'] = $brand['id'];
            $BaseCategory = D('BaseCategory');
            $category = $BaseCategory->where($con)->find();
            $condition['category_id'] = $category['id'];
        }
        $commodity = $_REQUEST['commodity_word'];
        $commodity = iconv('GB2312', 'UTF-8', $commodity);
        if (!empty($commodity)) {
            $con = array();
            $con['name'] = $commodity;
            $con['brand_id'] = $brand['id'];
            $con['category_id'] = $category['id'];
            $BaseCommodity = D('BaseCommodity');
            $commodity = $BaseCommodity->where($con)->find();
            $condition['commodity_id'] = $commodity['id'];
        }



        $share_info = $Share->relation(true)->where($condition)->select();

        $this->assign('share_info', $share_info);
        $this->display('search_favorites');
    }

    public function myFlow() {
        $this->checkLogin();
        $user_id = Session::getLocal('id');
        $UserFlow = D('UserFlow');
        //        该用户关注的
        $user_flow = $UserFlow->where("userid = '{$user_id}'")->select();
        //        关注该用户的
        $flow_user = $UserFlow->where("flow_userid = '{$user_id}'")->select();


        $str = array();
        $user_flow_id = array();
        $flow_user_id = array();
        if (count($user_flow) > 0) {
            foreach ($user_flow as $user) {
                $str[] = $user['flow_userid'];
                $user_flow_id[] = $user['flow_userid'];
            }
        }

        if (count($flow_user) > 0) {
            foreach ($flow_user as $user) {
                //                $str[]=$user['userid'];
                $flow_user_id[] = $user['userid'];
            }
        }

        $User = D('User');
        //        用户关注的会员
        $condition = array();
        $condition['id'] = array('in', $user_flow_id);
        $user_flow_info = $User->where($condition)->select();
        //        关注用户的会员
        $condition = array();
        $condition['id'] = array('in', $flow_user_id);
        $flow_user_info = $User->where($condition)->select();

        $condition = array();
        $condition['userid'] = array('in', $str);

        $Share = D('Share');
        import("ORG.Util.String"); // 导入字符串类
        import("ORG.Util.Page"); // 导入分页类

        $count = $Share->where($condition)->count(); // 查询满足要求的总记录数

        $Page = new Page($count, 10); // 实例化分页类传入总记录数和每页显示的记录数

        $show = $Page->show(); // 分页显示输出

        $list = $Share->where($condition)->order('time_add desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        if (intval($list[0]['id']) > 0) {
            for ($i = 0; $i < count($list); $i++) {
                $list[$i]['content'] = html_entity_decode($list[$i]['content']);
                $list[$i]['content'] = strip_tags($list[$i]['content']);
                $list[$i]['content'] = String:: msubstr($list[$i]['content'], $start = 0, 200);
                $list[$i]['User'] = $User->where("id = '{$list[$i]['userid']}'")->find();
            }
        }
//        print_r($list);
        $this->assign('flow_user_info', $flow_user_info);
        $this->assign('user_flow_info', $user_flow_info);
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display('my_flow');
    }

    public function myFlowByid() {
        $this->checkLogin();
        $user_id = Session::getLocal('id');
        $id = $_POST['id'];
        if (intval($id) <= 0) {
            echo 2;
        }
        $User = D('User');
        $UserFlow = D('UserFlow');
        //        该用户关注的
        $user_flow = $UserFlow->where("userid = '{$user_id}' and flow_userid = '{$id}'")->find();
        if (count($user_flow) <= 0) {
            echo 2;
        }
        $condition = array();
        $condition['userid'] = $id;
        $Share = D('Share');
        import("ORG.Util.String"); // 导入字符串类
        import("ORG.Util.AjaxPage"); // 导入分页类

        $count = $Share->where($condition)->count(); // 查询满足要求的总记录数
        $Page = new AjaxPage($count, 10, 'show_my_flow'); // 实例化分页类传入总记录数和每页显示的记录数
        $show = $Page->show(); // 分页显示输出
        $list = $Share->where($condition)->order('time_add desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        for ($i = 0; $i < count($list); $i++) {
            $list[$i]['content'] = html_entity_decode($list[$i]['content']);
            $list[$i]['content'] = strip_tags($list[$i]['content']);
            $list[$i]['content'] = String:: msubstr($list[$i]['content'], $start = 0, 200);
            $list[$i]['User'] = $User->where("id = '{$list[$i]['userid']}'")->find();
        }

        $this->assign('id', $id);
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display('my_flow_share_list');
    }

    public function flowMyByid() {
        $this->checkLogin();
        $user_id = Session::getLocal('id');
        $id = $_POST['id'];
        if (intval($id) <= 0) {
            echo 2;
        }
        $User = D('User');
        $UserFlow = D('UserFlow');
        //        该用户关注的

        $flow_user = $UserFlow->where("flow_userid = '{$user_id}' and userid ='{$id}' ")->select();
        if (count($flow_user) <= 0) {
            echo 2;
        }
        $condition = array();
        $condition['userid'] = $id;
        $Share = D('Share');
        import("ORG.Util.String"); // 导入字符串类
        import("ORG.Util.AjaxPage"); // 导入分页类

        $count = $Share->where($condition)->count(); // 查询满足要求的总记录数

        $Page = new AjaxPage($count, 10, 'show_flow_my'); // 实例化分页类传入总记录数和每页显示的记录数

        $show = $Page->show(); // 分页显示输出

        $list = $Share->where($condition)->order('time_add desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        for ($i = 0; $i < count($list); $i++) {
            $list[$i]['content'] = html_entity_decode($list[$i]['content']);
            $list[$i]['content'] = strip_tags($list[$i]['content']);
            $list[$i]['content'] = String:: msubstr($list[$i]['content'], $start = 0, 200);
            $list[$i]['User'] = $User->where("id = '{$list[$i]['userid']}'")->find();
        }


        $this->assign('id', $id);
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display('my_flow_share_list');
    }

    //    关注
    public function addFlow() {
        $this->checkLogin();
        $id = $_GET['id'];
        if (intval($id) <= 0) {
            echo '0';
            return false;
        }
        $User = D('User');
        $user_info = $User->where("id = '{$id}'")->find();
        if (count($user_info) <= 0) {
            echo '0';
            return false;
        }
        $condition = array();
        $condition['userid'] = Session::getLocal('id');

        if ($condition['userid'] == $id) {
            echo '2';
            return false;
        }

        $condition = array();
        $condition['userid'] = Session::getLocal('id');
        $condition['flow_userid'] = $id;
        $UserFlow = D('UserFlow');

        $flow_info = $UserFlow->where($condition)->find();
        if (count($flow_info) > 0) {
            echo '3';
            return true;
        }

        $data = array();
        $data['userid'] = Session::getLocal('id');
        $data['flow_userid'] = $id;
        $data['add_time'] = time();

        if ($UserFlow->data($data)->add()) {
            echo '1';
            return true;
        } else {
            echo '0';
            return false;
        }
    }

    public function delFavorite() {
        $this->checkLogin();
        $uid = Session::getLocal('id');
        if (empty($uid)) {
            echo 2;
            return false;
        }
        $id = $_GET['id'];
        if (intval($id) <= 0) {
            echo 2;
            return false;
        }
        $UserFavorites = D('UserFavorites');
        $favorites = $UserFavorites->where("userid = '{$uid}' and share_id = {$id}")->find();

        if (count($favorites) <= 0) {
            echo 2;
            return false;
        }

        if ($UserFavorites->where("userid = '{$uid}' and share_id = {$id}")->delete()) {
            echo 1;
        } else {
            echo 2;
        }

        return true;
    }

    public function showUpdate() {
        $this->checkLogin();
        $id = Session::getLocal('id');
        $User = D('User');
        $user_info = $User->where("id = '{$id}'")->find();
        //   $this->assign('user',$user_info);
        $this->display('show_update_user');
    }

    public function editUser() {
        $this->checkLogin();
        // print_r($_POST);
        $data = array();
        $User = D('User');
        $uid = Session::getLocal('id');
        $userinfo = $User->where("id = '{$uid}'")->find();

        if (md5($_POST['old_password']) == $userinfo['password']) {
            if ($_POST['new_password'] == $_POST['again_password']) {
                $data = array();
                $data['password'] = md5($_POST['new_password']);
                $rs = $User->where("id = '$uid'")->save($data);
                if (false === $rs) {
                    echo '<script>alert("操作失败");window.history.go(-1);</script>';
                    //                  echo 1;
                    return;
                } else {
                    //                    echo 0;
                    echo '<script>alert("操作成功");window.history.go(-1);</script>';
                    return;
                }
            }
        }

        echo '<script>alert("操作失败");window.history.go(-1);</script>';
    }

    public function showUpdateUser() {
        $this->checkLogin();
        $id = Session::getLocal('id');
        $User = D('User');
        $user_info = $User->where("id = '{$id}'")->find();
        $this->assign('user', $user_info);
        $this->display('my_upload_pic');
    }

    public function uploadPic() {
        $this->checkLogin();
        import("ORG.Net.UploadFile");
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 32922222;
        //设置上传文件类型
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');
        ;
        //设置附件上传目录
        $upload->savePath = './Public/Uploads/';
        // 文件重名自动覆盖
        $upload->uploadReplace = 'true';
        $upload->thumb = true;
        $upload->thumbMaxWidth = 200;
        $upload->thumbMaxHeight = 200;
        // 文件命名方式 time
        $upload->saveRule = 'time';

        if (!$upload->upload()) { // 上传错误提示错误信息
            echo '图片类型错误，请重试！';
        } else { // 上传成功获取上传文件信息
            $info = $upload->getUploadFileInfo();
            echo $info[0]['savename'];
        }
    }

    public function updateUserPic() {
        $this->checkLogin();
        $name = $_REQUEST['user_photo'];

        if (!$name) {
            echo 2;
            return false;
        }
        $data['user_photo'] = $name;
        $id = Session::getLocal('id');
        $User = D('User');
        $rs = $User->where('id=' . $id)->save($data);

        if ($rs) {
            echo 1;
            return true;
        }
        echo 3;
        return false;
    }

    public function delUserPic() {
        $this->checkLogin();
        $data['user_photo'] = '';
        $id = Session::getLocal('id');
        $User = D('User');
        $rs = $User->where('id=' . $id)->save($data);
        if ($rs) {
            echo 1;
            return true;
        }
        echo 3;
        return false;
    }

}

?>
